//
//  StrategyViewController.m
//  TrueLove
//
//  Created by han on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyViewController.h"
#import "UpStrategyCell.h"
#import "DownStrategyCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UpStrategy.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "DownStrategy.h"
#import "DownStrategyViewController.h"
#import "Button1ViewController.h"
#import "Button2ViewController.h"
#import "Button3ViewController.h"
@interface StrategyViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *ImageArr;
@property (nonatomic, retain) NSMutableArray *TextArr;
@property (nonatomic, retain) UIView *imageView;
@end

@implementation StrategyViewController
- (void)dealloc
{
    [_tableView release];
    [_ImageArr release];
    [_TextArr release];
    [_imageView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self showText];
    // Do any additional setup after loading the view.
    self.title = @"结婚攻略";
    self.navigationController.navigationBar.translucent = NO;
    NSString *str = @"http://hunliji.com/v1/api/app/wedding_raiders.json?location=front&page=1&per_page=10";
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager
    manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *tempArr = [responseObject objectForKey:@"wedding_raiders"];
      //  NSLog(@"%@", tempArr);
        self.ImageArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in tempArr) {
            UpStrategy *upstrategy = [[UpStrategy alloc] init];
            [upstrategy setValuesForKeysWithDictionary:dic];
            [self.ImageArr addObject:upstrategy];
            [upstrategy release];
            

        }
        

       // NSLog(@"%@", self.ImageArr);
        [self.tableView reloadData];
        
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        [self.view addSubview:self.tableView];
        [self showImage];
        [self.tableView release];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
    
    
}
//解析下方文字
- (void)showText
{
    NSString *str = @"http://hunliji.com/v1/api/app/wedding_raiders.json?location=&page=1&per_page=32767";
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *tempArr = [responseObject objectForKey:@"wedding_raiders"];
        self.TextArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in tempArr) {
            DownStrategy *downStrategy = [[DownStrategy alloc] init];
            [downStrategy setValuesForKeysWithDictionary:dic];
            [self.TextArr addObject:downStrategy];
            [downStrategy release];
        // NSLog(@"%@", downStrategy.title);
          //  NSLog(@"%@", self.TextArr);
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];

}
//上方三个图片
- (void)showImage
{
    
    self.imageView = [[[UIView alloc] initWithFrame:CGRectMake(0,10, self.view.frame.size.width, 150)] autorelease];
    self.imageView.backgroundColor = [UIColor cyanColor];
    self.tableView.tableHeaderView = self.imageView;
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.backgroundColor = [UIColor cyanColor];
    button1.frame = CGRectMake(20, 10, 90, 110);
    [button1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUpInside];

    UpStrategy *upstrategy = [self.ImageArr objectAtIndex:0];
    NSString *imageString = upstrategy.cover_path;
    NSURL *url = [NSURL URLWithString:imageString];
    [button1 setBackgroundImageWithURL:url forState:UIControlStateNormal];
    [self.imageView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.backgroundColor = [UIColor yellowColor];
    button2.frame = CGRectMake(140, 10, 90, 110);
    [button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUpInside];
    UpStrategy *upstrategy1 = [self.ImageArr objectAtIndex:1];
    NSString *imageString1 = upstrategy1.cover_path;
    NSURL *url1 = [NSURL URLWithString:imageString1];
    [button2 setBackgroundImageWithURL:url1 forState:UIControlStateNormal];
    [self.tableView addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.backgroundColor = [UIColor blueColor];
    button3.frame = CGRectMake(260, 10, 90, 110);
    [button3 addTarget:self action:@selector(button3:) forControlEvents:UIControlEventTouchUpInside];
    UpStrategy *upstrategy2 = [self.ImageArr objectAtIndex:2];
    NSString *imageString2 = upstrategy2.cover_path;
    NSURL *url2 = [NSURL URLWithString:imageString2];
    [button3 setBackgroundImageWithURL:url2 forState:UIControlStateNormal];

    [self.tableView addSubview:button3];

    
}
- (void)button1:(UIButton *)button1
{
    Button1ViewController *button1VC = [[Button1ViewController alloc] init];
    [self.navigationController pushViewController:button1VC animated:YES];
    [button1VC release];
}
- (void)button2:(UIButton *)button2
{
    Button2ViewController *button2VC = [[Button2ViewController alloc] init];
    [self.navigationController pushViewController:button2VC animated:YES];
    [button2VC release];
}
- (void)button3:(UIButton *)button3
{
    Button3ViewController *button3VC = [[Button3ViewController alloc] init];
    [self.navigationController pushViewController:button3VC animated:YES];
    [button3VC release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.TextArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str = @"str";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    
    DownStrategy *downStrategy = [self.TextArr objectAtIndex:indexPath.row];
//    NSLog(@"%@", self.TextArr);
//    NSLog(@"%@", downStrategy.title);
    cell.textLabel.text = downStrategy.title;
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
//跳转到下一个下方的详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownStrategyViewController *downStrVC = [[DownStrategyViewController alloc] init];
    DownStrategy *downStrategy = [self.TextArr objectAtIndex:indexPath.row];
    downStrVC.idStr =[NSString stringWithFormat:@"%@", downStrategy.id];
    
    [self.navigationController pushViewController:downStrVC animated:YES];
    [downStrVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
